Ext.ns('TaxonTagger');
TaxonTagger.gridpanel = function(config){
	var pagesize = 30;
	
	this.pagingBar = new Ext.PagingToolbar({
			store: TaxonTagger.docStore
		,	pageSize: pagesize
		,	displayInfo: true
		,	emptyMsg: this.emptyMsg
		,	items: ['-', {
					iconCls : 'icon_comments'	
				,	scope: this	
				,	tooltip: this.txtMenuCommentEdit
				,	handler: function(){
						var selSciname = Ext.getCmp('gridpanel').selModel.getSelections();
						if (selSciname.length > 0) {
							this.tmpRow = selSciname[0];
							if (!Ext.isDefined(this.tmpRow.data.orig)) {
								TaxonTagger.Notice.msg(this.msgNoticeTitle, this.msgNoticeNoSciAvail);
							} else {
								this.comments = new TaxonTagger.comments({
										title: this.txtComments + this.tmpRow.data.scientificName
									,	keyName: this.tmpRow.data.scientificName
								});
								this.comments.loadComment();
								this.comments.on("close", this.updateComment, this);
								this.comments.show();
							}
						} else {
							TaxonTagger.Notice.msg(this.msgNoticeTitle, this.msgNoticeNoSciSel);
						}
				}
		}]
	});

	var expander = new Ext.ux.grid.RowExpander({
		tpl: new Ext.XTemplate(
			'<div style="padding-left:4px; padding-bottom:2px;">'
			+ '<tpl if="taxonID != \'\'"><div><b>ID:</b> {taxonID}</div></tpl>'
			+ '<div><b>Scientific Name:</b> {scientificName}</div>'
			+ '<div><b>Verbatim:</b> {verbatim}</div>'
			+ '<tpl if="genus != \'\'"><div><b>Genus:</b> {genus}</div></tpl>'
			+ '<tpl if="family != \'\'"><div><b>Family:</b> {family}</div></tpl>'
			+ '<tpl if="order != \'\'"><div><b>Order:</b> {order}</div></tpl>'
			+ '<tpl if="phylum != \'\'"><div><b>Phylum:</b> {phylum}</div></tpl>'
			+ '<tpl if="kingdom != \'\'"><div><b>Kingdom:</b> {kingdom}</div></tpl>'
			+ '<tpl if="isSynonym != \'\'"><div><b>Synonym:</b> {isSynonym}</div></tpl>'
			+ '<tpl if="comment != \'\'"><div><b>Comment:</b> {comment}</div></tpl>'
			+ '</div>'
		)
	});

	var columns = [
			expander
		,	{header: 'Scientific Name', width: 140, sortable: true, dataIndex: 'scientificName'}
		,	{header: 'Species Full', width: 120, hidden: true, sortable: true, dataIndex: 'orig'}
		,	{header: 'Found By', width: 100, sortable: true, dataIndex: 'source'}
		,	{header: 'Comments', width: 83, sortable: true, dataIndex: 'comment'}
		,	{header: 'Freq.', width: 37, sortable: true, dataIndex: 'frequency'}
	]

	this.sm =	new Ext.grid.RowSelectionModel({
			singleSelect: true
		,	listeners: {
					rowselect: function(sm, row, rec){
						var f = Ext.getCmp('mifp').getFrameDocument().body;
						Ext.each(Ext.fly(f).select("span.tt-sciname").elements, function(el){
							var o2 = Ext.fly( this );
							if (el.attributes.getNamedItem("scientificname").value == rec.data.scientificName) {
								Ext.get(el).addClass('highlight_name');
								o2.next().addClass('highlight_name');
							}
						});
					}
				,	rowdeselect: function(sm, row, rec){
						var f = Ext.getCmp('mifp').getFrameDocument().body;
						Ext.each(Ext.fly(f).select("span.tt-sciname").elements, function(el){
							var o2 = Ext.fly( this );
							if (el.attributes.getNamedItem("scientificname").value == rec.data.scientificName) {
								Ext.get(el).removeClass('highlight_name');
								o2.next().removeClass('highlight_name');
							}
						});
					}
			}
	});

	Ext.apply(this, config, {
			id: 'gridpanel'	
		,	store: TaxonTagger.docStore
		,	columns: columns
		,	sm: this.sm
		,	plugins: expander
		,	animCollapse: false
		,	bbar: this.pagingBar
		,	iconCls: 'icon_table'
		,	listeners: {
				rowcontextmenu: this.showContextMenu
			}
	});

	TaxonTagger.gridpanel.superclass.constructor.call(this, config);

};

Ext.extend(TaxonTagger.gridpanel, Ext.grid.GridPanel, {

		showContextMenu: function(grid, row, e){
			grid.getSelectionModel().selectRow(row);
			var record = grid.getSelectionModel().getSelected().data;
			var items = [];
			var item4 = ({
					text: this.txtTaxamatch
				,	scope: this	
				,	handler: function() { 
						this.showTaxaMatch(grid, row);
					}  	
			});

			items.push({
					text: this.txtMenuCheckList
				,	ref: 'checkBank'
				,	scope: this	
				,	handler: function() { 
						window.open('http://ecat-dev.gbif.org/search?q=' + record.scientificName + '&rkey=1', '_blank');
					}  
			},{
					text: this.txtMenuCommentEdit
				,	iconCls: 'icon_comments'
				,	scope: this	
				,	handler: function() { 
						this.showComment(grid, row);
					}  	
			},{
					text: this.txtMenuRemoveSci + record.scientificName
				,	scope: this	
				,	handler: function() { 
						Ext.getCmp('mifp').fireEvent('removeName', this, record.scientificName, record.verbatim);
					}  	
			});
			if(record.source == 'Manual' && Config.useTaxamatch ){
				items.push(item4);
			}
			var menu = new Ext.menu.Menu({
				items: items
			});
			var xy = e.getXY();
			menu.showAt(xy);
		}
	
	,	updateComment: function( commentPanel, a ) {
			var data = this.getSelectionModel().getSelected().data;
			var index = TaxonTagger.docStore.findExact('scientificName',data.scientificName);
			var rec = TaxonTagger.docStore.getAt(index)
			rec.set('comment', commentPanel.txtComment.getValue());
			rec.commit();
			TaxonTagger.docStore.commitChanges();
			Ext.getCmp('list-delimited').loadList();
		}
	
	,	showComment: function(grid, row){
			var data = this.store.getAt(row).data;
			this.comments = new TaxonTagger.comments({
						title: this.txtComments + data.scientificName
					,	keyName: data.scientificName
				});
				this.comments.loadComment();
				this.comments.on("close", this.updateComment, this);
				this.comments.show();
		}

	,	showTaxaMatch: function(grid, row){
			var data = this.store.getAt(row).data;
			this.taxamatch = new TaxonTagger.TaxamatchLookup({
					title: this.titleShowTaxamatch + '(' + data.verbatim + ')'
				,	keyName: data.verbatim	
			});
			this.taxamatch.txtSearch.setValue(data.verbatim);	
			this.taxamatch.show();
		}

});